#version 400 core

#define POSITION	0
#define NORMAL		1
#define COLOR		2
#define TEXCOORD	3
#define FRAG_COLOR	0
#define FRAG_COLOR_AD0 1
#define FRAG_COLOR_AD1 2
#define FRAG_COLOR_AD2 3

uniform sampler2D normalSampler;
uniform sampler2D heightSampler;
uniform float spacing;

layout(location = FRAG_COLOR) out vec4 COLOR_OUT;

in vec2 fTexcoord;
in float fHeight;
in vec3 fPos;

void main()
{
	float heightCentre  = textureOffset(heightSampler, fTexcoord, ivec2(0.0, 0.0)).r; 
    float heightLeft    = textureOffset(heightSampler, fTexcoord, ivec2(-1.0, 0.0)).r; 
    float heightRight   = textureOffset(heightSampler, fTexcoord, ivec2(1.0, 0.0)).r; 
    float heightBottom  = textureOffset(heightSampler, fTexcoord, ivec2(0.0, -1.0)).r; 
    float heightTop     = textureOffset(heightSampler, fTexcoord, ivec2(0.0, 1.0)).r; 

    vec3 lr = vec3(0.5f, heightRight - heightLeft, 0.0);
    vec3 bt = vec3(0.0, heightTop - heightBottom, 0.5f);
    vec3 normal = normalize(cross(lr, bt));

	vec3 lightDir = normalize(vec3(0.1, -1.0, 0.0));

    float LDotN = max(0.0, dot(lightDir, normal));

    float heightAdjusted = max(0.0, (10.0 + fHeight)*0.015);

    //COLOR_OUT = vec4(heightAdjusted, LDotN*LDotN, 0.0, 1.0);
    COLOR_OUT = vec4(vec3(LDotN), 1.0);

	//COLOR_OUT = vec4(1.0, 0.0, 0.0, 1.0);

    //COLOR_OUT = vec4(1.0, 1.0, 1.0, 1.0);

    float tileSpacing = 75.0;
    ivec2 t = ivec2(fTexcoord.x*tileSpacing, fTexcoord.y*tileSpacing);
    // xor
    if ( ((t.x % 2 == 0) && (t.y % 2 != 0)) ||
         ((t.x % 2 != 0) && (t.y % 2 == 0)) )
        COLOR_OUT = vec4(0.3, 0.25, 0.4, 1.0);
    else 
        COLOR_OUT = vec4(0.3, 0.65, 0.3, 1.0);
    //COLOR_OUT = vec4(fTexcoord, 1.0, 1.0);

    //COLOR_OUT = vec4(fTexcoord.x, fTexcoord.y, 0.0, 1.0);

    //COLOR_OUT = vec4(-normal.rgb, 1.0);

    //COLOR_OUT = vec4(1.0, 1.0, 1.0, 1.0);
}
